package com.panshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panshi.entity.AppMatterT;
import com.panshi.service.impl.AppMatterTServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 问题表 前端控制器
 * </p>
 *
 * @author xhlMr
 * @since 2020-08-04
 */
@RestController
public class AppMatterTController {

    @Autowired
    private AppMatterTServiceImpl appMatterTService;

    //乐观锁增加浏览量
    @GetMapping("/app-matter-t/matterReadAdd/{matterId}")
    public Boolean matterReadAdd(@PathVariable int matterId){
        AppMatterT byId = appMatterTService.getById(matterId);
        // todo: redis缓存. 防止阅读重复
        byId.setReadCount(byId.getReadCount()+1);
        return appMatterTService.updateById(byId);
    }

    //增加评论量
    @GetMapping("/app-matter-t/matterCommAdd/{matterId}")
    public Boolean matterCommAdd(@PathVariable int matterId){
        AppMatterT byId = appMatterTService.getById(matterId);
        byId.setCommentCount(byId.getCommentCount()+1);
        return appMatterTService.updateById(byId);
    }


    @ApiOperation("查询全部问答")
    @GetMapping("/app-matter-t/all")
    public List<AppMatterT> all(){
        return appMatterTService.list(null);
    }

    @ApiOperation("按ID查询问答")
    @GetMapping("/app-matter-t/matById/{id}")
    public AppMatterT matById(@PathVariable("id") int id){
        return appMatterTService.getById(id);
    }

    @ApiOperation("问答指定分页查询页数与个数  cid分类  page咨询页数 ，limit 多少  type 按排序")
    @GetMapping("/app-matter-t/qaPage/{cid}/{page}/{limit}/{type}")
    public List<AppMatterT> qaPage(@PathVariable int cid,@PathVariable int page, @PathVariable int limit, @PathVariable String type){
        if (page < 1){
            page = 1;
        }
        // 查询这个分类下的所有问题，获取查询的数据信息
        Page<AppMatterT> pageParam = new Page<>(page, limit);
        if(cid == 0){
            appMatterTService.page(pageParam, new QueryWrapper<AppMatterT>().orderByDesc(type,"ground_time"));
        }else{
            appMatterTService.page(pageParam, new QueryWrapper<AppMatterT>().eq("classify_id",cid).orderByDesc(type,"ground_time"));
        }

        List<AppMatterT> records = pageParam.getRecords();
        return records;
    }


    @ApiOperation("问答指定分页查询页数与个数  cid分类  page咨询页数 ，limit 多少  type answerCount 按已回答未回答")
    @GetMapping("/app-matter-t/qaPage/{cid}/{page}/{limit}/{type}/{answerCount}")
    public List<AppMatterT> qaAnTypePage(@PathVariable int cid,@PathVariable int page, @PathVariable int limit, @PathVariable String type,@PathVariable int answerCount){
        if (page < 1){
            page = 1;
        }
        // 查询这个分类下的所有问题，获取查询的数据信息
        Page<AppMatterT> pageParam = new Page<>(page, limit);

        if(cid == 0){
            if(answerCount>0){
                appMatterTService.page(pageParam, new QueryWrapper<AppMatterT>().gt("answer_count",1).orderByDesc(type,"ground_time"));
            }else if(answerCount==0){
                appMatterTService.page(pageParam, new QueryWrapper<AppMatterT>().eq("answer_count",answerCount).orderByDesc(type,"ground_time"));
            }

        }else{
            if(answerCount>0){
                appMatterTService.page(pageParam, new QueryWrapper<AppMatterT>().gt("answer_count",1).eq("classify_id",cid).orderByDesc(type,"ground_time"));
            }else if(answerCount==0){
                appMatterTService.page(pageParam, new QueryWrapper<AppMatterT>().eq("answer_count",answerCount).eq("classify_id",cid).orderByDesc(type,"ground_time"));
            }

        }

        List<AppMatterT> records = pageParam.getRecords();
        return records;
    }
}

